package org.jeecg.modules.sistersophie.util;

import com.itextpdf.text.pdf.BaseFont;
import com.lowagie.text.DocumentException;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.config.StaticConfig;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

/**
 * PDF工具类
 * Created by sitou on 2017/10/15.
 */
public class FileUtil {

    private String path = "font:/usr/data/font/simsun.ttc";

    public void createPDF(String htmlContent, OutputStream os) throws DocumentException, IOException {
        StaticConfig staticConfig = SpringContextUtils.getBean(StaticConfig.class);
        String fontPath = staticConfig.getFileFont();
        System.out.println("fontPath:"+fontPath);
        if(StringUtils.isBlank(fontPath)){
            fontPath = path;
        }
        System.out.println("fontPath:"+fontPath);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(htmlContent);
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        SharedContext sharedContext = renderer.getSharedContext();
        // 解决base64图片支持问题
        sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory());
        sharedContext.getTextRenderer().setSmoothingThreshold(0);

        renderer.layout();
        renderer.createPDF(os,true);
    }
    /**
     * 根据html字符串创建PDF
     * @param htmlContent
     */
    public FileOutputStream createPDF(String htmlContent, String filePath) throws IOException, DocumentException {
        StaticConfig staticConfig = SpringContextUtils.getBean(StaticConfig.class);
        String fontPath = staticConfig.getFileFont();
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(htmlContent);
        ITextFontResolver fontResolver = renderer.getFontResolver();
        fontResolver.addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        SharedContext sharedContext = renderer.getSharedContext();
        // 解决base64图片支持问题
        sharedContext.setReplacedElementFactory(new B64ImgReplacedElementFactory());
        sharedContext.getTextRenderer().setSmoothingThreshold(0);
        renderer.layout();
        renderer.createPDF(fileOutputStream,true);
        fileOutputStream.close();
        return fileOutputStream;
    }

    /**
     * 创建HTML类型文件
     * @param htmlContent
     * @param filePath
     * @throws IOException
     */
    public FileOutputStream createHTML(String htmlContent,String filePath) throws IOException {
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        OutputStreamWriter oStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
        oStreamWriter.append(htmlContent);
        oStreamWriter.close();
        return fileOutputStream;
    }

}
